const express = require('express')
const router = express.Router()
const WebSocket = require('../db/model/WebSocketModel')
const { mySend, Vtoken, checkParams } = require('../utils/index')
const { to } = require('await-to-js')
const {refresh} = require('../utils/webSocketMonitor')

router.post('/add', async (req, res) => {
    const { siteName, secret, url, token } = req.body
    let checked = checkParams({ siteName, secret })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let query = {
        userId: user._id,
        username: user.username,
        siteName,
        secret,
    }

    if(url){
        query.url = url
    }

    let [err, result] = await to(WebSocket.insertMany(query))
    if(err) return mySend(res, 1, '添加失败', err)
    return mySend(res, 0, '添加成功', result)
})

router.post('/delete', async (req, res) => {
    const { _id, token } = req.body
    let checked = checkParams({ _id, token })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(WebSocket.deleteOne({_id}))
    if(err) return mySend(res, 1, '删除失败', err)
    if(result.deletedCount === 0) return mySend(res, 1, '删除失败', result)
    return mySend(res, 0, '删除成功', result)
})

router.post('/update', async (req, res) => {
    const {token, _id} = req.body
    let checked = checkParams({ _id})
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(WebSocket.updateOne({_id: req.body._id}, {...req.body}))
    if(err) return mySend(res, 1, '修改失败', err)
    if(result.nModified === 0) return mySend(res, 1, '你没有修改任何数据?', result)
    return mySend(res, 0, '修改成功', result)
})

router.post('/list', async (req, res) => {
    const {token, current = 1, size = 10, status} = req.body
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let query = {userId: user._id}
    if(status) query.status = status

    let [err, total] = await to(WebSocket.countDocuments(query))
    let [err1, result] = await to(WebSocket.find(query).sort({ctime: -1}).skip((current - 1) * size).limit(size))
    if(err1) return mySend(res, 1, '查询失败', err1)
    let body = {
        total,
        current, 
        size,
        list: result
    }
    return mySend(res, 0, 'success', body)
})

router.post('/listOne', async (req, res) => {
    const {token, _id} = req.body
    let checked = checkParams({ _id})
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(WebSocket.find({_id, userId: user._id}))
    if(err) return mySend(res, 1, '查询失败', err)
    return mySend(res, 0, 'success', result[0])
})

//不传userId, 查询所有用户,传userId查指定用户
router.post('/listAll', async (req, res) => {
    const {token, userId, current = 1, size = 10} = req.body
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')
    if(user.role !== 1) return mySend(res, 1, '你没有权限查看')

    let query = {}
    if(userId) query.userId = userId
    let [err, total] = await to(WebSocket.countDocuments({query}))
    let [err1, result] = await to(WebSocket.find(query).sort({userId: -1, ctime: -1}).skip((current - 1) * size).limit(size))
    if(err) return mySend(res, 1, '查询失败', err)
    let body = {
        total,
        current, 
        size,
        list: result
    }
    return mySend(res, 0, 'success', body)
})

router.post('/refresh', async(req, res) => {
    const {token, _id} = req.body
    let checked = checkParams({token, _id})
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')
    
    let [err, result] = await to(WebSocket.findOne({_id, userId: user._id}))
    if(err) mySend(res, 1, '刷新失败', err)
    if(!result) return mySend(res, 1, '找不到要刷新的记录', result)
    refresh(result.secret)
    return mySend(res, 0, 'success')
})

module.exports = router

